package JDBCUI;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class query extends JFrame {

    private static final long serialVersionUID = -1928970409928880648L;
    JLabel jlpno = new JLabel("奖惩       编号：");
    JLabel jlname = new JLabel("奖惩类型(备注)：");
    JLabel jlaward = new JLabel("奖惩     金额：");
    JLabel jlreason = new JLabel("奖惩    缘由：");

    JTextField jtpno = new JTextField("",20);

    JLabel jname = new JLabel();
    JLabel jaward = new JLabel();
    JLabel jreason = new JLabel();




    JButton buttonask = new JButton("查询奖惩编号信息");
    JButton buttonreturn = new JButton("返回");

    public query() {
        JPanel jppno = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpaward = new JPanel();
        JPanel jpreason= new JPanel();


        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jppno.add(jlpno);
        jppno.add(jtpno);

        jpname.add(jlname);
        jpname.add(jname);

        jpaward.add(jlaward);
        jpaward.add(jaward);

        jpreason.add(jlreason);
        jpreason.add(jreason);



        jpforbutton.add(buttonask);
        jpforbutton.add(buttonreturn);
        buttonask.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                Connection conn = null;
                ResultSet res = null;
                Statement stat = null;

                String xpno = jtpno.getText();

                String sql = "SELECT pno,name,award,reason FROM staff where pno ='"+xpno+"'  ;";
                try{
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    System.out.println("jdbc haha");
                }catch(Exception d){
                    System.out.println("jdbc fall");
                    d.printStackTrace();
                }

                try{
                    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","123456");
                    stat=conn.createStatement();
                    res=stat.executeQuery(sql);

                    if (!res.next()){
                        if(e.getSource()==buttonask){
                            jname.setText("");
                            jaward.setText("");
                            jreason.setText("");
                            javax.swing.JOptionPane.showMessageDialog(null, "不存在，查询失败！");
                        }
                    }

                    res=stat.executeQuery(sql);

                    while (res.next())
                    {
                        String i = res.getString(1).trim();

                        System.out.println(i);

                        if (i.equals(jtpno.getText()))
                        {
                            jname.setText(res.getString(2));
                            jaward.setText(res.getString(3));
                            jreason.setText(res.getString(4));
                            break;
                        }
                    }




                }catch (SQLException e1) {
                    e1.printStackTrace();


                }
                finally{
                    try{
                        conn.close();

                    }catch(SQLException ar){
                        ar.printStackTrace();
                    }

                }}}

        );
        buttonreturn.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                dispose();
            }
        });
        this.setTitle("教师情况条");
        this.setLayout(new GridLayout(9,1));
        this.add(jppno);
        this.add(jpname);
        this.add(jpaward);
        this.add(jpreason);

        this.add(jpforbutton);
        this.setLocation(800,300);
        this.setSize(500,400);
        this.setVisible(true);

    }
}
